package com.example.ws;

import com.example.service.flowable.IEvectionService;
import com.example.service.flowable.impl.EvectionServiceImpl;
import com.example.util.Constant;
import com.example.vo.ResultVo;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 出差申请任务通知
 *
 * @author wsl
 */
@ServerEndpoint(value = "/task/evection",configurator = GetHttpSessionConfigurator.class)
@Component
public class EvectionTaskNotice {

    //用来存储每一个客户端对象对应的ChatEndpoint对象
    private static Map<String,EvectionTaskNotice> onlineUsers = new ConcurrentHashMap<>();

    //声明session对象，通过该对象可以发送给指定的用户
    private Session session;

    //声明一个HttpSession对象，我们之前在HttpSession对象中存储了用户名
    private HttpSession httpSession;


    @OnOpen
    //连接建立时被调用
    public void onOpen(Session session, EndpointConfig config) throws IOException, EncodeException {
        //将当前局部的session赋值给成员session
        this.session = session;
        //获取httpSession对象
        HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
        this.httpSession = httpSession;

        //从httpSession对象中获取用户名
        String username = (String) httpSession.getAttribute(Constant.CURRENT_USER);
        String userId = httpSession.getAttribute(Constant.CURRENT_USER_ID).toString();
        //将当前对象存储到容器中
        onlineUsers.put(username,this);
        IEvectionService evectionService = new EvectionServiceImpl();
        ResultVo resultVo = evectionService.queryEvectionAuditTask(userId);

        //将当前在线用户的用户名推送给所有的客户端
        //1、获取消息
//        String message = MessageUtils.getMessage(false,null,getNames());
        //2、调用方法进行系统消息的推送
        Set<String> names = onlineUsers.keySet();
//        evectionService.queryEvectionAuditTask()
        for (String name : names) {
            EvectionTaskNotice evectionTaskNotice = onlineUsers.get(name);
            evectionTaskNotice.session.getBasicRemote().sendObject(resultVo);
        }
    }

    private Set<String> getNames(){
        return onlineUsers.keySet();
    }

    @OnMessage
    //接收到客户端发送的数据时被调用
    public void onMessage(String message, Session session){
//        try {
//            //将message转换成message对象
//            ObjectMapper mapper = new ObjectMapper();
//            Message mess = mapper.readValue(message, Message.class);
//            //获取要将数据发送给的用户
//            String toName = mess.getToName();
//            //获取消息数据
//            String data = mess.getMessage();
//            //获取当前登录的用户
//            String username = (String) httpSession.getAttribute(Constant.CURRENT_USER);
//            //获取推送给指定用户的消息格式
//            String resultMassage = MessageUtils.getMessage(false,username,data);
            //发送数据
//            onlineUsers.get(toName).session.getBasicRemote().sendText(resultMassage);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }



    @OnClose
    //连接关闭时被调用
    public void onClose(){

    }
}
